package com.chinaunicom.club.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.chinaunicom.club.model.Activity;
import com.chinaunicom.club.service.IActivity;

@Controller
@RequestMapping("/club2/activity")
public class ActivityController2{
	
	@Resource
	public IActivity service2;
	
	/**
	 * 查询所有的活动信息
	 * @param model
	 * @return
	 */
	@RequestMapping("/queryAll")
	public String queryAll(Model model){
		//1.从数据库中取出所有的活动信息
		List<Activity> activities = service2.queryAll(null);		
		//2.将活动信息传递到jsp页面
		model.addAttribute("activities", activities);
		return "/WEB-INF/club2/activity/list.jsp";
	}
	
	/**
	 * 查看单个活动信息
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/viewDetail")
	public String viewDetail(Model model,int id){
		//1.从数据库中取出该ID的活动信息
		Activity activity = service2.selectByPrimaryKey(id);
		//2.将活动信息传递到JSP页面
		model.addAttribute("activity", activity);
		return "/WEB-INF/club2/activity/viewDetail.jsp";
	}
	
	/**
	 * 跳转到添加活动信息页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/toAdd")
	public String toAdd(Model model){
		return "/WEB-INF/club2/activity/toAdd.jsp";
	}

	/**
	 * 保存新增的活动信息到数据库
	 * @param model
	 * @param activity
	 * @return
	 */
	@RequestMapping("/add")
	public String add(Model model,Activity activity){
		service2.insertSelective(activity);
		return "/club2/activity/queryAll";
	}
	
	
	/**
	 * 跳转到修改活动信息页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/toUpdate")
	public String toUpdate(Model model,int id){
		//1.从数据库中取出该ID的活动信息
		Activity activity = service2.selectByPrimaryKey(id);
		//2.将活动信息传递到JSP页面
		model.addAttribute("activity", activity);
		return "/WEB-INF/club2/activity/toUpdate.jsp";
	}
	
	/**
	 * 保存修改后的活动信息到数据库
	 * @param model
	 * @param activity
	 * @return
	 */
	@RequestMapping("/save")
	public String save(Model model,Activity activity){
		service2.updateByPrimaryKeySelective(activity);
		return "/club2/activity/queryAll";
	}
	
	/**
	 * 将该活动进行隐式删除
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/del")
	public String del(Model model,int id){
		Activity activity = new Activity();
		activity.setId(id);
		activity.setIsDel(0);
		service2.updateByPrimaryKeySelective(activity);
		return "/club2/activity/queryAll";
	}
	
	/**
	 * 跳转到审核活动信息的页面
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/toCheck")
	public String toCheck(Model model,int id){
		Activity activity = service2.selectByPrimaryKey(id);
		model.addAttribute("activity", activity);
		return "/WEB-INF/club2/activity/toCheck.jsp";
	}
	
	/**
	 * 根据审核结果，修改活动信息的审核状态
	 * @param model
	 * @param id
	 * @param checkStatus
	 * @return
	 */
	@RequestMapping("/check")
	public String Check(Model model,int id,int checkStatus){
		service2.check(id,checkStatus);
		return "/club2/activity/queryAll";
	}
	
	/**
	 * 导出所有的活动信息
	 * @param request
	 * @param response
	 */
	@RequestMapping("/exportAllActivities")
	public void exportAllActivities(HttpServletRequest request,HttpServletResponse response){
		//1.调用方法，将活动信息填充到excel表格中，并提供下载
		service2.exportAllActivities(request,response);
	}
	
	/**
	 * 导出单个活动信息
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/exportActivityById")
	public void exportActivityById(int id,HttpServletRequest request,HttpServletResponse response){
		boolean flag = service2.exportActivityById(id,request,response);
	}
}
